package my.fuelwallet.test;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CarProfileManagerActivity extends ListActivity {
    /** Called when the activity is first created. */
	
	private CarProfileHandle profiles;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.car_profile_manager);
        
        profiles = new CarProfileHandle(this);
        showNameList();
    }
    
    private void showNameList(){
    	profiles.open();
        List<String> values = profiles.getAccountNameList();
        if(values.isEmpty()){
        	alertNameListEmpty();
        	 //CarProfile profile_test = new CarProfile();
             //profiles.putCarProfile(profile_test);
        }
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
        profiles.close();
    }
    
    private void alertNameListEmpty(){
    	Builder MyAlertDialog = new AlertDialog.Builder(this);
    	MyAlertDialog.setTitle("No profile exists");
    	MyAlertDialog.setMessage("Please create a new car profile first.");
    	DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener()
    	{
    		public void onClick(DialogInterface dialog, int which) {
    			Intent intent = new Intent(CarProfileManagerActivity.this, AddProfile.class );
    			startActivity(intent);
    		}
    	};
    	MyAlertDialog.setNeutralButton("OK",OkClick );
    	MyAlertDialog.show();
    	
    }
    
    public void onClick(View view) {
    	switch (view.getId()) {
    	case R.id.add_profile:
    		Intent intent = new Intent(CarProfileManagerActivity.this, AddProfile.class );
    		startActivity(intent);
    		break;
    	/*case R.id.open_map:
    		Intent intent2 = new Intent(CarProfileManagerActivity.this, BaseMapActivity.class );
    		startActivity(intent2);*/
    	}
    }
    
    @Override  
    protected void onListItemClick(ListView l, View v, int position, long id) {  
    	
    	String accountName = l.getItemAtPosition(position).toString();
    	Intent intent = new Intent(CarProfileManagerActivity.this, ViewProfile.class );
    	Bundle bundle = new Bundle(); 
    	bundle.putString("accountName", accountName);
    	intent.putExtras(bundle);
		startActivity(intent);
      
		super.onListItemClick(l, v, position, id);
    }
    
    @Override
	protected void onResume() {
    	showNameList();
		super.onResume();
	}
}